package com.ruoyi.project.data.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.data.domain.WeightManagement;
import com.ruoyi.project.data.service.IWeightManagementService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 管理制度文本与组织结构图Controller
 *
 * @author ruoyi
 * @date 2020-04-26
 */
@RestController
@RequestMapping("/data/weightManagement")
public class WeightManagementController extends BaseController
{
    @Autowired
    private IWeightManagementService weightManagementService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询管理制度文本与组织结构图列表
     */
    @PreAuthorize("@ss.hasPermi('data:weightManagement:list')")
    @GetMapping("/list")
    public TableDataInfo list(WeightManagement weightManagement)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            weightManagement.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightManagement.setAreas(areas);
        weightManagement.setManagementCharacters(managementCharacters);
        startPage();
        List<WeightManagement> list = weightManagementService.selectWeightManagementList(weightManagement);
        return getDataTable(list);
    }

    /**
     * 导出管理制度文本与组织结构图列表
     */
    @PreAuthorize("@ss.hasPermi('data:weightManagement:export')")
    @Log(title = "管理制度文本与组织结构图", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WeightManagement weightManagement)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            weightManagement.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightManagement.setAreas(areas);
        weightManagement.setManagementCharacters(managementCharacters);
        List<WeightManagement> list = weightManagementService.selectWeightManagementList(weightManagement);
        ExcelUtil<WeightManagement> util = new ExcelUtil<WeightManagement>(WeightManagement.class);
        return util.exportExcel(list, "weightManagement");
    }

    /**
     * 获取管理制度文本与组织结构图详细信息
     */
    @PreAuthorize("@ss.hasPermi('data:weightManagement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(weightManagementService.selectWeightManagementById(id));
    }

    /**
     * 新增管理制度文本与组织结构图
     */
    @PreAuthorize("@ss.hasPermi('data:weightManagement:add')")
    @Log(title = "管理制度文本与组织结构图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WeightManagement weightManagement)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station")
                || permissionService.hasRole("xq_station") || permissionService.hasRole("jy_station") || permissionService.hasRole("yx_station"))){
            Long stationId = user.getStationId();
            weightManagement.setStationId(stationId);
        }else{
            return AjaxResult.error("当前角色无新增权限");
        }
        if (StringUtils.isNotBlank(weightManagement.getType()) && "2".equals(weightManagement.getType())){
            List<WeightManagement> list = weightManagementService.selectWeightManagementList(new WeightManagement(user.getStationId(),weightManagement.getType()));
            if (list != null && list.size() >0){
                return AjaxResult.error("组织结构图已存在，请勿重复添加");
            }
        }
        return toAjax(weightManagementService.insertWeightManagement(weightManagement));
    }

    /**
     * 修改管理制度文本与组织结构图
     */
    @PreAuthorize("@ss.hasPermi('data:weightManagement:edit')")
    @Log(title = "管理制度文本与组织结构图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WeightManagement weightManagement)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station")
                || permissionService.hasRole("xq_station") || permissionService.hasRole("jy_station") || permissionService.hasRole("yx_station"))){
            Long stationId = user.getStationId();
            weightManagement.setStationId(stationId);
        }else{
            return AjaxResult.error("当前角色无修改权限");
        }
        if ("1".equals(weightManagement.getType())){
            weightManagement.setPhotoPath(" ");
        }else if ("1".equals(weightManagement.getType())){
            weightManagement.setContent(" ");
        }
        return toAjax(weightManagementService.updateWeightManagement(weightManagement));
    }

    /**
     * 删除管理制度文本与组织结构图
     */
    @PreAuthorize("@ss.hasPermi('data:weightManagement:remove')")
    @Log(title = "管理制度文本与组织结构图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(weightManagementService.updateWeightManagementById(ids));
    }
}
